package leetcode;

import java.util.*;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0128.java
 * @Author : WangRuoyu
 * @Date 2023/11/27 21:03
 */

public class Q0128 {
    public int longestConsecutive(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int i : nums) {
            set.add(i);
        }
        List<Integer> list = new ArrayList<>(set);
        list.sort(Comparator.comparingInt(o -> o));
        int res = 0;
        int j = 0;
        for (int i = 0; i < list.size(); i++) {
            if (i - j == list.get(i) - list.get(j)) {
                res = Math.max(res, i - j + 1);
            } else {
                j = i;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Q0128 sl = new Q0128();
    }
}